//
//  LRStatuesTransmitCell.swift
//  LRWeibo
//
//  Created by 雷路荣 on 15/12/22.
//  Copyright © 2015年 leilurong. All rights reserved.
//

/**
*  转发微博cell
*/
import UIKit

class LRStatuesTransmitCell: LRStatuesCell {
    // MARK: - 属性
    /// 微博模型,用于显示转发微博的文本和转发微博的图片
    /// 覆盖父类的属性 1.添加 override 关键字, 2.必须实现属性监视器 3.先调用父类的属性监视器,在调用子类的属性监视器
    override var statues: LRStatues? {
        didSet{
            // 获取被转发微博模型
            if let transmitStatues = statues?.retweeted_status {
                // 设置被转发微博的内容
                // 1.被转发为微博的文本 =  "@"被转发微博的作者: 被转发微博的内容
//                // 设置被转发微博的文本内容 加“ ！”不然名字和微博正文会有@optional
//                transmitLabel.text = "@\(transmitStatues.user!.screen_name!):\(transmitStatues.text!)"
//                let name = transmitStatues.user?.screen_name ?? "被转发微博名称为空!"
//                let retweetText = transmitStatues.text ?? "被转发微博内容为空!"
//                let text = "@" + name + ":" + retweetText
//                transmitLabel.text = text
                
                //名字
                let name = transmitStatues.user!.screen_name ?? "被转发微博名称为空!"
                //属性文本
                let attrText = transmitStatues.attrText
                // 将名称和带表情图片的属性文本拼接
                let attrM = NSMutableAttributedString(string: name)
                attrM.appendAttributedString(attrText!)
                transmitLabel.attributedText = attrM
            }
        }
    }
    //重写父类方法
    override func prepareUI() {
        super.prepareUI()
        //添加子控件
        contentView.addSubview(backGroundButton)
        contentView.addSubview(transmitLabel)
        //将背景按钮移到最底部
        contentView.sendSubviewToBack(backGroundButton)
        //设置约束
        /// 背景按钮,左上角,在微博正文label的底部左边
        backGroundButton.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: nil, offset: CGPointMake(-LRStatusCellMargin, LRStatusCellMargin))
        /// 背景按钮,右下角,在底部视图右上角
        backGroundButton.ff_AlignVertical(type: ff_AlignType.TopRight, referView: cellBottomView, size: nil)
        /// 被转发微博文本label,在背景按钮内部,左上角
        transmitLabel.ff_AlignInner(type: ff_AlignType.TopLeft, referView: backGroundButton, size: nil, offset: CGPointMake(LRStatusCellMargin, LRStatusCellMargin))
        /// 被转发微博文本label宽度
        contentView.addConstraint(NSLayoutConstraint(item: transmitLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.width-2*LRStatusCellMargin))
        //微博配图view
        let constant = cellPictrueView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: transmitLabel, size: CGSize(width: 298, height: 298), offset: CGPoint(x: 0, y: LRStatusCellMargin))
        //获取宽高约束
        pictureViewWidthConstraint = cellPictrueView.ff_Constraint(constant, attribute: NSLayoutAttribute.Width)
        pictureViewHeightConstraint = cellPictrueView.ff_Constraint(constant, attribute: NSLayoutAttribute.Height)
        
    }
    //MARK: - 转发微博cell点击,实际点击的是背景按钮
    func retweetStatusBackGroundButtonClick() {
        //print("retweetStatusBackGroundButtonClick")
        delegate?.retweetedStatusDidClick(statues!.retweeted_status!)
    }
    
    //MARK - 懒加载------------------------------
    ///背景按钮
    private lazy var backGroundButton: UIButton = {
        let button = UIButton()
        button.backgroundColor = UIColor(white: 0.88, alpha: 1)
        button.addTarget(self, action: "retweetStatusBackGroundButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
       return button
    }()
    /// 转发微博的文本内容
    private lazy var transmitLabel: UILabel = {
        let label = UILabel(fontSize: 14, textColor: UIColor.darkGrayColor())
        label.numberOfLines = 0
        //设置折行文本宽度(换行的宽度)，不然cell之间会随机出现间距
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width-2*8
        return label
    }()
}
